package com.doge.calcalbum.business

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.doge.calcalbum.business.account.AppUserViewModel
import com.doge.calcalbum.business.album.picture.AlbumPictureFragment
import com.doge.calcalbum.business.album.video.AlbumVideoFragment
import com.doge.calcalbum.business.calc.CalcController
import com.doge.calcalbum.business.calc.PolicyDialogFragment
import com.doge.calcalbum.business.guide.GuideAlbum1Layout
import com.doge.calcalbum.business.guide.GuideNoteLayout
import com.doge.calcalbum.business.guide.GuideSkinLayout
import com.doge.calcalbum.business.home.NavigationLayout
import com.doge.calcalbum.business.home.ProfileFragment
import com.doge.calcalbum.business.vip.VipHelper
import com.doge.calcalbum.business.skin.SkinFragment
import com.doge.calcalbum.business.sysalbum.SysAlbumRepository
import com.doge.calcalbum.business.trash.TrashRepository
import com.doge.calcalbum.business.update.UpdateHelper
import com.doge.calcalbum.core.BaseActivity
import com.doge.calcalbum.core.BaseFragment
import com.doge.calcalbum.databinding.ActivityMainBinding
import com.doge.calcalbum.utils.AppGuideUtils
import com.doge.calcalbum.utils.appContext
import com.doge.calcalbum.utils.setOnClickCallback
import com.doge.calcalbum.utils.updateSystemWindowInsetsTop

class MainActivity : BaseActivity() {
    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    private val viewModel by viewModels<PolicyViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        dispatchApplyWindowInsets()
        initialViews()

        SysAlbumRepository.get().refresh()
        VipHelper.tryShowWhenAppOpen(this)
        VipHelper.tryRestoreSkin()
        TrashRepository().clearExpired()
        UpdateHelper.check()
        AppUserViewModel.get().requestSelfInfo()

        viewModel.agreeEvent.observe(this) {
            it?.also { GuideAlbum1Layout.attach(window.decorView as ViewGroup) }
        }
        PolicyDialogFragment.tryShowPolicyDialogInFirstTime(this)
    }

    private fun initialViews() {
        binding.viewPager.offscreenPageLimit = 4
        binding.viewPager.isUserInputEnabled = false
        binding.viewPager.adapter =
            object : androidx.viewpager2.adapter.FragmentStateAdapter(this) {
                override fun getItemCount(): Int = 4
                override fun createFragment(position: Int): Fragment {
                    return when (position) {
                        0 -> AlbumPictureFragment.newInstance()
                        1 -> AlbumVideoFragment.newInstance()
                        2 -> SkinFragment()
                        3 -> ProfileFragment()
                        else -> BaseFragment()
                    }
                }
            }

        binding.navigation.attach(binding.viewPager)
        binding.navigation.setListener(object : NavigationLayout.Callback {
            override fun onItemClick(position: Int): Boolean {
                binding.viewPager.setCurrentItem(position, false)

                if (position == 0) {
                    if (!AppGuideUtils.hasGuideAlbum)
                        GuideAlbum1Layout.attach(window.decorView as ViewGroup)
                }

                if (position == 2) {
                    if (AppGuideUtils.hasGuideAlbum) {
                        GuideSkinLayout.attach(window.decorView as ViewGroup)
                        // binding.skinTip.setDisplayed()
                    }
                }
                if (position == 3) {
                    if (!AppGuideUtils.hasGuideSetPwd)
                        CalcController.notifyIntercept(true)
                    else if (AppGuideUtils.hasGuideAlbum)
                        GuideNoteLayout.attach(window.decorView as ViewGroup)
                }
                return false
            }

            override fun onItemClickAgain(position: Int) {
            }
        })

//        binding.skinTip.setOnClickCallback {
//            binding.viewPager.setCurrentItem(2, false)
//            GuideSkinLayout.attach(window.decorView as ViewGroup)
//            binding.skinTip.setDisplayed()
//        }
    }

    private fun dispatchApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, insets ->
            updateSystemWindowInsetsTop(insets.systemWindowInsets.top)
            insets
        }
    }

    override fun onResume() {
        super.onResume()
        VipHelper.tryShowWhenSetPwdFirstTime(this)
    }

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, MainActivity::class.java).apply {
                if (context == appContext) flags = Intent.FLAG_ACTIVITY_NEW_TASK
            })
        }
    }
}
